home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 September / CHIP Eylül 1998.iso / Slackwar / docs / mini / Bridge+Firewall < prev    next >
Text File  |  1997-12-28  |  20KB  |  521 lines

  1.   Linux Bridge+Firewall Mini-HOWTO version 1.2.0
  2.   Peter Breuer (ptb@it.uc3m.es)
  3.   Dec. 19 1997
  4.  
  5.   1.  Introduction
  6.  
  7.   You should look at the original Bridging mini-HOWTO
  8.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/mini/Bridge> by Chris Cole
  9.   for a different perspective on this.  He is chris@polymer.uakron.edu.
  10.   The version of his HOWTO that I have based this document on
  11.   (alternatively, ripped off) is 1.03 dated Aug 23 1996.
  12.  
  13.   2.  What and Why (and How?)
  14.  
  15.   2.1.  What
  16.  
  17.   A bridge is an intelligent connecting wire betwen two network cards.
  18.   A firewall is an intelligent insulator.
  19.  
  20.   2.2.  Why
  21.  
  22.   You might want a bridge if you have several computers:
  23.  
  24.   1. to save the price of a new hub when you just happen to have an
  25.      extra ethernet card available.
  26.  
  27.   2. to save the bother of learning how to do IP-forwarding and other
  28.      tricks when you _have_ two cards in your computer.
  29.  
  30.   3. to avoid maintenance work in the future when things change around!
  31.  
  32.   ``Several computers'' might be as few as three if those are routing or
  33.   bridging or just moving around the room from time to time! You also
  34.   might want a bridge just for the fun of finding out what it does.
  35.   ``2'' was what I wanted a bridge for.
  36.  
  37.   If you are really interested in ``1'', you have to be one of the very
  38.   few.  Check the NET-2-HOWTO
  39.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/NET-2-HOWTO> and the
  40.   Serial-HOWTO <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Serial-HOWTO>
  41.   for better tricks.
  42.  
  43.   You want a firewall if
  44.  
  45.   1. you are trying to protect your network from external accesses, or
  46.  
  47.   2. you are trying to deny access to the world outside from your
  48.      network.
  49.  
  50.   Curiously, I needed ``2'' here too.  Policy at my university presently
  51.   is that we should not act as internet service providers to
  52.   undergraduates.
  53.  
  54.   2.3.  How?
  55.  
  56.   I started out bridging the network cards in a firewalling machine and
  57.   ended up firewalling without having cut the bridge. It seems to work
  58.   and is more flexible than either configuration alone. I can take down
  59.   the firewall and keep bridging or take down the bridge when I want to
  60.   be more circumspect.
  61.  
  62.   I would guess that the bridge code lives just above the physical
  63.   device layer and the firewalling code lives one layer higher up, so
  64.   that the bridging and firewalling configurations effectively act as
  65.   though they are running connected together ``in sequence'' and not
  66.   ``in parallel'' (ouch!). Diagram:
  67.  
  68.        -> Bridge-in -> Firewall-in -> Kernel -> Firewall-out -> Bridge-out ->
  69.  
  70.   There is no other way to explain how one machine can be a
  71.   ``conductor'' and an ``insulator'' at the same time.  There are a few
  72.   caveats but I'll come to those later.  Basically you must route
  73.   packets that you want to firewall.  Anyway, it all seems to work
  74.   together nicely for me.  Here is what you do ...
  75.  
  76.   3.  BRIDGING
  77.  
  78.   3.1.  Software
  79.  
  80.   Get the bridge configuration utility
  81.   <ftp://shadow.cabi.net/pub/Linux/BRCFG.tgz> from Alan Cox's home
  82.   pages. This is the same reference as in Chris' document. I just didn't
  83.   realize that it was an ftp and not an http URL ...
  84.  
  85.   3.2.  Prior Reading.
  86.  
  87.   Read the Multiple Ethernet HOWTO
  88.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/mini/Multiple-Ethernet>
  89.   for some advice on getting more than one network card recognized and
  90.   configured.
  91.  
  92.   Yet more details of the kind of boot magic that you may need are in
  93.   the Boot Prompt HOWTO
  94.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/BootPrompt-HOWTO>.
  95.  
  96.   You may be able to get away without the NET-2 HOWTO
  97.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/NET-2-HOWTO>. It is a good
  98.   long read and you will have to pick from it the details you need.
  99.  
  100.   3.3.  Boot configuration
  101.  
  102.   The reading material above will tell you that you need to prepare the
  103.   kernel to recognize a second ethernet device at boot up by adding this
  104.   to your /etc/lilo.conf, and then re-run lilo:
  105.  
  106.        append = 'ether=0,0,eth1'
  107.  
  108.   Note the 'eth1'. 'eth0' is the first card. 'eth1' is the second card.
  109.   You can always add the boot parameters in your response to the line
  110.   that lilo offers you. This is for three cards:
  111.  
  112.        linux ether=0,0,eth1 ether=0,0,eth2
  113.  
  114.   I use loadlin to boot my kernel from DOS:
  115.  
  116.        loadlin.exe c:\vmlinuz root=/dev/hda3 ro ether=0,0,eth1 ether=0,0,eth2
  117.  
  118.   Note that this trick makes the kernel probe at bootup. That will not
  119.   happen if you load the ethernet drivers as modules (for safety since
  120.   the probe order can't be determined) so if you use modules you will
  121.   have to add the appropriate IRQ and port parameters for the driver in
  122.   your /etc/conf.modules.  I have at least
  123.  
  124.        alias eth0 3c509
  125.        alias eth1 de620
  126.        options 3c509 irq=5 io=0x210
  127.        options de620 irq=7 bnc=1
  128.  
  129.   You can tell if you use modules by using ``ps -aux'' to see if kerneld
  130.   is running and checking that there are .o files in a subdirectory of
  131.   your /lib/modules directory. You want the directory named with what
  132.   uname -r tells you. If you have kerneld and/or you have a foo.o then
  133.   edit /etc/conf.modules and read the man page for depmod carefully.
  134.  
  135.   Note also that until recently (kernel 2.0.25) the 3c509 driver could
  136.   not be used for more than one card if used as a module. I have seen a
  137.   patch floating around that fixes the oversight. It may be in the
  138.   kernel when you read this.
  139.  
  140.   3.4.  Kernel configuration
  141.  
  142.   Recompile the kernel with bridging enabled.
  143.  
  144.   CONFIG_BRIDGE=y
  145.  
  146.   I also compiled with firewalling and IP-forwarding and -masquerading
  147.   and the rest enabled. Only if you want firewalling too ...
  148.  
  149.        CONFIG_FIREWALL=y
  150.        CONFIG_NET_ALIAS=y
  151.        CONFIG_INET=y
  152.        CONFIG_IP_FORWARD=y
  153.        CONFIG_IP_MULTICAST=y
  154.        CONFIG_IP_FIREWALL=y
  155.        CONFIG_IP_FIREWALL_VERBOSE=y
  156.        CONFIG_IP_MASQUERADE=y
  157.  
  158.   You don't need all of this. What you do need apart from this is the
  159.   standard net configuration:
  160.  
  161.        CONFIG_NET=y
  162.  
  163.   and I do not think you need worry about any of the other networking
  164.   options. I have any options that I did not actually compile into the
  165.   kernel available through kernel modules that I can add in later.
  166.  
  167.   Install the new kernel in place, rerun lilo and reboot with the new
  168.   kernel. Nothing should have changed at this point!
  169.  
  170.   3.5.  Network addresses
  171.  
  172.   Chris says that a bridge should not have an IP address but that is not
  173.   the setup to be described here.
  174.  
  175.   You are going to want to use the machine for connecting to the net so
  176.   you need an address and you need to make sure that you have the
  177.   loopback device configured in the normal way so that your software can
  178.   talk to the places they expect to be able to talk to. If loopback is
  179.   down the name resolver or other net sevices might fail. See the
  180.   NET-2-HOWTO, but your standard configuration should already have done
  181.   this bit:
  182.  
  183.        ifconfig lo 127.0.0.1 route add -net 127.0.0.0
  184.  
  185.   You will have to give addresses to your network cards. I altered the
  186.   /etc/rc.d/rc.inet1 file in my slackware (3.x) to setup two cards and
  187.   you should also essentially just look for your net configuration file
  188.   and double or treble the number of instructions in it.  Suppose that
  189.   you already have an address at
  190.  
  191.        192.168.2.100
  192.  
  193.   (that is in the private net reserved address space, but never mind -
  194.   it won't hurt anybody if you use this address by mistake) then you
  195.   probably already have a line like
  196.  
  197.        ifconfig eth0 192.168.2.100 netmask 255.255.255.0 metric 1
  198.  
  199.   in your configuration. The first thing you are going to probably want
  200.   to do is cut the address space reached by this card in half so that
  201.   you can eventually bridge or firewall the two halves.  So add a line
  202.   which reduces the mask to address a smaller number of machines:
  203.  
  204.        ifconfig eth0 netmask 255.255.255.128
  205.  
  206.   Try it too. That restricts the card to at most the address space
  207.   between
  208.  
  209.   Now you can set your second card up in the other half of the local
  210.   address space. Make sure that nobody already has the address. For
  211.   symmetry I set it at 228=128+100 here. Any address will do so long as
  212.   it is not in the other card's mask, and even then, well, maybe. Avoid
  213.   special addresses like .0, .1, .128 etc. unless you really know what
  214.   you are doing.
  215.  
  216.        ifconfig eth1 192.168.2.228 netmask 255.255.255.128 metric 1
  217.  
  218.   That restricts the second card to addresses between .128 and .255.
  219.  
  220.   3.6.  Network routing
  221.  
  222.   This is where I have to announce the caveats in the bridging +
  223.   firewalling scheme: you cannot firewall packets which are not routed.
  224.   No routes, no firewall. At least this appears to be true in the 2.0.30
  225.   and more recent kernels. The firewalling filters are closely involved
  226.   with the ip-forwarding code.
  227.  
  228.   That does not mean that you cannot bridge. You can bridge between two
  229.   cards and firewall them from a third. You can have only two cards and
  230.   firewall both of them against an outside IP such as a nearby router,
  231.   provided that the router is routed by you to exactly one of the cards.
  232.  
  233.   In other words, since I will be doing firewalling, so I want to
  234.   precisely control the physical destination of some packets.
  235.  
  236.   I have the small net of machines attached to a hub hanging off eth0,
  237.   so I configure a net there:
  238.  
  239.        route add -net 192.168.2.128 netmask 255.255.255.128 dev eth0
  240.  
  241.   The 128 would be 0 if I had a full class C network there.  I don't, by
  242.   definition, since I just halved the address space.  The 'dev eth0' is
  243.   not necessary here because the cards address falls within the mask,
  244.   but it may be necessary for you.  One might need more than one card
  245.   holding up this subnet (127 machines on one segment, oh yeah) but
  246.   those cards would be being bridged under the same netmask so that they
  247.   appear as one to the routing code.
  248.  
  249.   On the other card I have a line going straight through to a big router
  250.   that I trust.
  251.  
  252.                                                client 129
  253.            __                                        |    __
  254.   client 1   \    .0                    .128         |   /   net 1
  255.   client 2 --- Hub - eth0 - Kernel - eth1 - Hub - Router --- net 2
  256.   client 3 __/       .100            .228         .2 |   \__ net 3
  257.                                                      |
  258.                                                client 254
  259.  
  260.   I attach the address of the router to that card as a fixed ('static')
  261.   route because it would otherwise fall within the first cards netmask
  262.   and the kernel would be thinking wrongly about how to send packets to
  263.   the big router. I will want to firewall these packets and that is
  264.   another reason fow wanting to route them specifically.
  265.  
  266.        route add 192.168.2.2 dev eth1
  267.  
  268.   I don't need it, since I don't have any more machines in that half of
  269.   the address space, but I declare a net also on the second card.
  270.   Separating my interfaces into two sets via routing will allow me to do
  271.   very tight firewalling eventually , but you can get away with far less
  272.   routing than this.
  273.  
  274.        route add -net 192.168.2.128 netmask 255.255.255.128 dev eth1
  275.  
  276.   I also need to send all non-local packets out to the world so I tell
  277.   the kernel to send them to the big router
  278.  
  279.        route add default gw 192.168.2.2
  280.  
  281.   3.7.  Card configuration
  282.  
  283.   So much was standard networking setup, but we are bridging so we also
  284.   have to listen on both (?) cards for packets that are not aimed at us.
  285.   The following should go into the network configuration file.
  286.  
  287.        ifconfig promisc eth0 ifconfig promisc eth1
  288.  
  289.   The man page says allmulti=promisc, but it didn't work for me.
  290.  
  291.   3.8.  Additional routing
  292.  
  293.   One thing that I noticed was that I had to put at least the second
  294.   card into a mode where it would respond to the big router's questions
  295.   about which machines I was hiding in my local net.
  296.  
  297.        ifconfig arp eth1
  298.  
  299.   For good measure I did this to the other card too.
  300.  
  301.        ifconfig arp eth0.
  302.  
  303.   3.9.  Bridge Configuration
  304.  
  305.   Put bridging enabling on and into your configuration file:
  306.  
  307.        brcfg -enable
  308.  
  309.   You should have been trying this out in real time all along, of
  310.   course!  The bridge configure will bring up some numbers. You can
  311.   experiment with turning on and off the ports one at a time
  312.  
  313.        brcfg -port 0 -disable/-enable
  314.         brcfg -port 1 -disable/-enable
  315.  
  316.   You get status reports anytime by just running
  317.  
  318.        brcfg
  319.  
  320.   without any parameters. You will see that the bridge listens,learns,
  321.   and then does forwarding. (I don't understand why the code repeats the
  322.   same hardware addresses for both my cards, but never mind .. Chris'
  323.   howto say that is OK)
  324.  
  325.   3.10.  Try it out
  326.  
  327.   If you are still up and running as things are, try out your
  328.   configuration script for real by taking down both cards and then
  329.   executing it:
  330.  
  331.        ifconfig eth0 down ifconfig eth1 down /etc/rc.d/rc.inet1
  332.  
  333.   With any luck the various subsystems (nfs, ypbind, etc.)  won't
  334.   notice. Do not try this unless you are sitting at the keyboard!
  335.  
  336.   If you want to be more careful than this, you should take down as many
  337.   daemons as possible beforehand, and unmount nfs directories. The worst
  338.   that can happen is that you have to reboot in single-user mode (the
  339.   'single' parameter to lilo or loadlin), and take out your changes
  340.   before rebooting with things the way they were before you started.
  341.  
  342.   3.11.  Checks
  343.  
  344.   Verify that there is different traffic on each interface:
  345.  
  346.        tcpdump -i eth0
  347.  
  348.        tcpdump -i eth1
  349.  
  350.   You should get used to using tcpdump to look for things that should
  351.   not be happening or that are happening and should not.
  352.  
  353.   For instance look for packets that have gone through the bridge to the
  354.   second card from the internal net. Here I am looking for packets from
  355.   the machine with address .22:
  356.  
  357.        tcpdump -i eth1 -e host 192.168.2.22
  358.  
  359.   Then send a ping from the .22 host to the router. You should see the
  360.   packet reported by tcpdump.
  361.  
  362.   At this stage you should have a bridge ready that also has two network
  363.   addreses.  Test that you can ping them from outside and inside your
  364.   local net, and that you can telnet and ftp around between inside and
  365.   outside too.
  366.  
  367.   4.  FIREWALLING
  368.  
  369.   4.1.  Software and reading
  370.  
  371.   You should read the Firewall-HOWTO
  372.   <ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/Firewall-HOWTO>.
  373.  
  374.   That will tell you where to get ipfwadm if you don't already have it.
  375.   There are other tools you can get but I made no progress until I tried
  376.   ipfwadm. It is nice and low level! You can see exactly what it is
  377.   doing.
  378.  
  379.   4.2.  Preliminary checks
  380.  
  381.   You have compiled IP-forwarding and masquerading into the kernel so
  382.   you will want to check that the firewall is in its default (accepting)
  383.   state with
  384.  
  385.        ipfwadm -I -l ipfwadm -O -l ipfwadm -F -l
  386.  
  387.   That is respectively, 'display the rules affecting the ..'  incoming
  388.   or outgoing or forwarding (masquerading) '.. sides of the firewall'.
  389.   The '-l' means 'list'.
  390.  
  391.   You might have compiled in accounting too:
  392.  
  393.        ipfwadm -A -l
  394.  
  395.   You should see that there are no rules defined and that the default is
  396.   to accept every packet. You can get back to this working state anytime
  397.   with
  398.  
  399.        ipfwadm -I -f
  400.        ipfwadm -O -f
  401.        ipfwadm -F -f
  402.  
  403.   The '-f' means 'flush'. You may need to use that.
  404.  
  405.   4.3.  Default rule
  406.  
  407.   I want to cut the world off from my internal net and do nothing else,
  408.   so I will want to give as a last (default) rule that the firewall
  409.   should ignore any packets coming in from the internal net and directed
  410.   to outside.  I put all the rules (in this order) into
  411.   /etc/rc.d/rc.firewall and execute it from /etc/rc.d/rc.local at
  412.   bootup.
  413.  
  414.        ipfwadm -I -a reject -S 192.168.2.0/255.255.255.128 -D 0.0.0.0/0.0.0.0
  415.  
  416.   The '-S' is the source address/mask. The '-D' is the destination
  417.   address/mask.
  418.  
  419.   This format to is rather long-winded. Ipfwadm is intelligent about
  420.   network names and some common abbreviations. Check the man pages.
  421.  
  422.   It is possibly more convenient to put some or all of these rules on
  423.   the outgoing half of the firewall by using '-O' instead of '-I', but
  424.   I'll state the rules here all formulated for the incoming half.
  425.  
  426.   4.4.  Holes per address
  427.  
  428.   Before that default rule, I have to place some rules that serve as
  429.   exceptions to this general denial of external services to internal
  430.   clients.
  431.  
  432.   I want to treat the firewall machines address on the internal net
  433.   specially.  I will stop people logging in to the firewall machine
  434.   unless they have special permission, but once they are there they
  435.   should be allowed to talk to the world.
  436.  
  437.        ipfwadm -I -i accept -S 192.168.2.100/255.255.255.255 \
  438.         -D 0.0.0.0/0.0.0.0
  439.  
  440.   I also want the internal clients to be able to talk to the firewalling
  441.   machine. Maybe they can persuade it to let them get out!
  442.  
  443.        ipfwadm -I -i accept -S 192.168.2.0/255.255.255.128 \
  444.         -D 192.168.2.100/255.255.255.255
  445.  
  446.   Check at this point that you can get in to the clients from outside
  447.   the firewall via telnet, but that you cannot get out. That should mean
  448.   that you can just about make first contact, but the clients cannot
  449.   send you any prompts. You should be able to get all the way in if you
  450.   use the firewall machine as a staging post. Try rlogin and ping too,
  451.   with tcpdump running on one card or the other. You should be able to
  452.   make sense of what you see.
  453.  
  454.   4.5.  Holes per protocol
  455.  
  456.   I went on to relax the rules protocol by protocol. I want to allow
  457.   pings from the outside to the inside to get an echo back, for
  458.   instance, so I inserted the rule:
  459.  
  460.        ipfwadm -I -i accept -P icmp -S 192.168.2.0/255.255.255.128 \
  461.         -D 0.0.0.0/0.0.0.0
  462.  
  463.   The '-P icmp' works the protocol-specific magic.
  464.  
  465.   Until I get hold of an ftp proxy I am also allowing ftp calls out with
  466.   port-specific relaxations. This targets ports 20 21 and 115 on outside
  467.   machines.
  468.  
  469.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 \
  470.         -D 0.0.0.0/0.0.0.0 20 21 115
  471.  
  472.   I could not make sendmail between the local clients work without a
  473.   nameserver. Rather than set up a nameserver right then on the
  474.   firewall, I just lifted the firewall for tcp domain service queries
  475.   precisely aimed at the nearest existing nameserver and put its address
  476.   in the clients /etc/resolv.conf ('nameserver 123.456.789.31' on a
  477.   separate line).
  478.  
  479.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 \
  480.         -D 123.456.789.31/255.255.255.255 54
  481.  
  482.   You can find which port number and protocol a service requires with
  483.   tcpdump. Trigger the service with a an ftp or a telnet or whatever to
  484.   or from the internal machine and then watch for it on the input and
  485.   output ports of the firewall with tcpdump:
  486.  
  487.        tcpdump -i eth1 -e host client04
  488.  
  489.   for example. The /etc/services file is another important source of
  490.   clues. To let telnet and ftp IN to the firewall from outside, you have
  491.   to allow the local clients to call OUT on a specific port. I
  492.   understand why this is necessary for ftp - it's the server that
  493.   establishes the data stream in the end - but I am not sure why telnet
  494.   also needs this.
  495.  
  496.        ipfwadm -I -i accept -P tcp -S 192.168.2.0/255.255.255.128 ftp telnet \
  497.         -D 0.0.0.0/0.0.0.0
  498.  
  499.   There is a particular problem with some daemons that look up the
  500.   hostname of the firewalling machine in order to decide what is their
  501.   networking address. Rpc.yppasswdd is the one I had trouble with. It
  502.   insists on broadcasting information that says it is outside the
  503.   firewall (on the second card). That means the clients inside can't
  504.   contact it.
  505.  
  506.   Rather than start IP aliasing or change the daemon code, I mapped the
  507.   name to the inside card address on the clients in their /etc/hosts.
  508.  
  509.   4.6.  Checks
  510.  
  511.   You want to test that you can still telnet, rlogin and ping from the
  512.   outside. From the inside you should be able to ping out. You should
  513.   also be able to telnet to the firewall machine from the inside and the
  514.   latter should be able to do anything.
  515.  
  516.   That is it. At this point you probably want to learn about rpc/Yellow
  517.   Pages and the interaction with the password file. The firewalled
  518.   network wants to run without its unprivileged users being able to log
  519.   on to the firewall - and thus get out. Some other HOWTO!
  520.  
  521.